let defaultOption = {
  isNotLocalEnv: false,
  token: '',
  shbVersion: ''
}

function shbLoginMiddleware(options = defaultOption) {
  return async function(ctx, next){
    await shbLoginMiddlewareHandler(ctx, next, options)
  }
}

async function shbLoginMiddlewareHandler(ctx, next, options) {
  let request = ctx.request;
  
  // 判断是否是本地环境
  if (options.isNotLocalEnv) {
    let headers = request.headers;
    
    // 设置cookie
    headers.cookie = `${headers.cookie}PUB_TOKEN=${options.token};PUB_VERSION=${options.shbVersion};`
    // 设置token & shbVersion
    headers.token = options.token;
    headers.shbVersion = options.shbVersion;
  } else {
    // TODO
  }
  
  await next();
}


module.exports = {
  shbLoginMiddleware
}
  